﻿/**
 * @file		ct_dd_pmc_watchdog.c
 * @brief		PMC Watchdog driver Component Test
 * @note		None
 * @attention	None
 * 
 * <B><I>Copyright 2016 Socionext Inc.</I></B>
 */

#include <stdlib.h>
#include <string.h>
#include "driver_common.h"
#include "dd_pmc_watchdog.h"

/*----------------------------------------------------------------------*/
/* Definition															*/
/*----------------------------------------------------------------------*/
//Nothing Special

/*----------------------------------------------------------------------*/
/* Enumeration															*/
/*----------------------------------------------------------------------*/
//Nothing Special

/*----------------------------------------------------------------------*/
/* Structure															*/
/*----------------------------------------------------------------------*/
//Nothing Special

/*----------------------------------------------------------------------*/
/* Global Data															*/
/*----------------------------------------------------------------------*/
//Nothing Special

/*----------------------------------------------------------------------*/
/* Local Method Definition												*/
/*----------------------------------------------------------------------*/
//Nothing Special

/*----------------------------------------------------------------------*/
/* Local Function														*/
/*----------------------------------------------------------------------*/
static VOID test_callback(VOID)
{
	UCHAR	raw_wdog_int;
	UCHAR	wdog_int;
	T_DD_PMC_WDOG_CTRL wdog_ctrl;
	
	Ddim_Print(("Watchdog Timer Interrupt\n"));
	Dd_PMC_Wdog_Get_Interrupt(&raw_wdog_int, &wdog_int);
	Ddim_Print(("WdogRIS = 0x%08x\n", raw_wdog_int));
	Ddim_Print(("WdogMIS = 0x%08x\n", wdog_int));

	wdog_ctrl.reset_enable = 0;
	wdog_ctrl.int_enable = 0;
	Dd_PMC_Wdog_Set_Control(&wdog_ctrl);
	Ddim_Print(("Dd_PMC_Wdog_Set_Control start\n"));
	
	Dd_PMC_Wdog_Get_Control(&wdog_ctrl);
	Ddim_Print(("WdogControl.RESEN = 0x%08x\n", wdog_ctrl.reset_enable));
	Ddim_Print(("WdogControl.INTEN = 0x%08x\n", wdog_ctrl.int_enable));

}

/*----------------------------------------------------------------------*/
/* Global Function														*/
/*----------------------------------------------------------------------*/
/**
 * @brief  Command main function for watchdog timer test.
 * @param  INT32 argc	:The number of parameters
 *		   UCHAR** argv	:The value of parameters
 *
 *			The meaning of parameters
 *			+------+------------+-------------+---------------+----------------------------------------------------+
 *			| P1   | P2         | P3          | P4            |Meaning                                             |
 *			+------+------------+-------------+---------------+----------------------------------------------------+
 *			| 1    |            |             |               |Initialize Watchdog timer Register.                 |
 *			+------+------------+-------------+---------------+----------------------------------------------------+
 *			| 2    |            |             |               |Exclusively controll of Watchdog timer Open/Close.  |
 *			+------+------------+-------------+---------------+----------------------------------------------------+
 *			| 3    |wdog_load   |reset_enable |int_enable     |Watchdog timer Controller.                          |
 *			+------+------------+-------------+---------------+----------------------------------------------------+
 *			| 4    |wdog_wdmode |msec         |               |Watchdog timer Timer Set.                           |
 *			+------+------------+-------------+---------------+----------------------------------------------------+
 *			| 5    |            |             |               |Watchdog timer Get API.                             |
 *			+------+------------+-------------+---------------+----------------------------------------------------+
 *			| 6    |            |             |               |Watchdog timer Error Test.                          |
 *			+------+------------+-------------+---------------+----------------------------------------------------+
 *			| 7    |            |             |               |Watchdog timer Interrupt Handler                    |
 *			+------+------------+-------------+---------------+----------------------------------------------------+
 *			| 8    |            |             |               |Watchdog timer API Sequence1.                       |
 *			+------+------------+-------------+---------------+----------------------------------------------------+
 *			| 9    |            |             |               |Watchdog timer API Sequence2.                       |
 *			+------+------------+-------------+---------------+----------------------------------------------------+
 *
 * @return VOID
 */

VOID Ct_Dd_PMC_WDOG_Main(int argc, char** argv)
{
	INT32  	ercd;
	UCHAR	raw_wdog_int;
	UCHAR	wdog_int;
	UCHAR	wdog_lock;
	UCHAR	test_mode;
	ULONG	wdog_load;
	ULONG	wdog_counter;
	ULONG	wdog_wdmode;
	ULONG	msec;
	ULONG	pcell;
	T_DD_PMC_WDOG_PERI_IDENTIFICATION	identification;
	T_DD_PMC_WDOG_CTRL					wdog_ctrl;
	
	switch ( strtol(argv[1], NULL, 16) ) {
		case 1:		// Init
			Ddim_Print(("Dd_PMC_Wdog_Init Test Start\n"));
			Dd_PMC_Wdog_Init();
			
			// WdogLock
			Dd_PMC_Wdog_Get_Lock(&wdog_lock);
			Ddim_Print(("WdogLock = 0x%08x (set 0x00000001)\n", wdog_lock));
			// WdogITCR
			Dd_PMC_Wdog_Get_Test_Mode(&test_mode);
			Ddim_Print(("WdogITCR = 0x%08x (set 0x00000000)\n", test_mode));
			// WdogControl
			Dd_PMC_Wdog_Get_Control(&wdog_ctrl);
			Ddim_Print(("WdogControl.RESEN = 0x%08x (set 0x00000000)\n", wdog_ctrl.reset_enable));
			Ddim_Print(("WdogControl.INTEN = 0x%08x (set 0x00000000)\n", wdog_ctrl.int_enable));
			
			Ddim_Print(("Dd_PMC_Wdog_Init Test End\n"));
			break;
			
		case 2:		// lock
			Ddim_Print(("Dd_PMC_Wdog_Set_Lock Test Start\n"));
			wdog_lock = strtoul(argv[3], NULL, 16);
			Dd_PMC_Wdog_Set_Lock(wdog_lock);									/* pgr0539 */
			Ddim_Print(("set lock : 0x%08x\n", wdog_lock));
		
			Dd_PMC_Wdog_Get_Lock(&wdog_lock);
			Ddim_Print(("WdogLock = 0x%08x\n", wdog_lock));
			break;
		case 3:		// Ctrl
			Ddim_Print(("Dd_PMC_Wdog_Ctrl Test Start\n"));
			wdog_ctrl.wdog_load = strtoul(argv[2], NULL, 16);
			wdog_ctrl.reset_enable = strtoul(argv[3], NULL, 16);
			wdog_ctrl.int_enable = strtoul(argv[4], NULL, 16);
			wdog_ctrl.pIntCallback = test_callback;
			
			if( wdog_ctrl.wdog_load <= 0 ){
				wdog_ctrl.wdog_load = 0;
			}
			if( wdog_ctrl.reset_enable <= 0 ){
				wdog_ctrl.reset_enable = 0;
			}
			if( wdog_ctrl.int_enable <= 0 ){
				wdog_ctrl.int_enable = 0;
			}
			if( Dd_PMC_Wdog_Ctrl(&wdog_ctrl) == D_DDIM_OK ){
				// WdogLoad
				Dd_PMC_Wdog_Get_Load(&wdog_load);
				Ddim_Print(("WdogLoad = 0x%08x\n", wdog_load));
				// WdogValue
				Dd_PMC_Wdog_Get_Counter(&wdog_counter);
				Ddim_Print(("WdogValue = 0x%08x\n", wdog_counter));
				// WdogControl
				Dd_PMC_Wdog_Get_Control(&wdog_ctrl);
				Ddim_Print(("WdogControl.RESEN = 0x%08x\n", wdog_ctrl.reset_enable));
				Ddim_Print(("WdogControl.INTEN = 0x%08x\n", wdog_ctrl.int_enable));
			}
			else{
				Ddim_Print(("please check parameter!!\n"));
			}
			
			Ddim_Print(("Dd_PMC_Wdog_Ctrl Test End\n"));
			
			break;
			
		case 4:		// Timer Set
			Ddim_Print(("Dd_PMC_Wdog_Set_Timer Test Start\n"));
			wdog_wdmode = strtoul(argv[2], NULL, 16);
			msec = strtoul(argv[3], NULL, 16);
			
			if( wdog_wdmode <= 0 ){
				wdog_wdmode = 0;
			}
			if( msec <= 0 ){
				msec = 0;
			}
			if( Dd_PMC_Wdog_Set_Timer(wdog_wdmode, msec) == D_DDIM_OK ){
				// WdogLoad
				Dd_PMC_Wdog_Get_Load(&wdog_load);
				Ddim_Print(("WdogLoad = 0x%08x\n", wdog_load));
				// WdogValue
				Dd_PMC_Wdog_Get_Counter(&wdog_counter);
				Ddim_Print(("WdogValue = 0x%08x\n", wdog_counter));
			}
			
			Ddim_Print(("Dd_PMC_Wdog_Set_Timer Test End\n"));
			
			break;
			
		case 5:		// Get API
			// WdogPeriphID0~3
			Dd_PMC_Wdog_Get_Peri_Identification(&identification);
			Ddim_Print(("WdogPeriphID Get part_number = 0x%08x\n", identification.part_number));
			Ddim_Print(("WdogPeriphID Get designer = 0x%08x\n", identification.designer));
			Ddim_Print(("WdogPeriphID Get revision_number = 0x%08x\n", identification.revision_number));
			Ddim_Print(("WdogPeriphID Get configuration = 0x%08x\n", identification.configuration));
			// WdogPCellID0~3
			Dd_PMC_Wdog_Get_PCell_Identification(&pcell);
			Ddim_Print(("WdogPCellID Get WdogPCellID[3:0] = 0x%08x\n", pcell));
			
			break;
			
		case 6:		// Error Test
			Ddim_Print(("Dd_PMC_Wdog_Ctrl Error Test\n"));
			ercd = Dd_PMC_Wdog_Ctrl(NULL);
			Ddim_Print(("Dd_PMC_Wdog_Ctrl Error = 0x%08x\n", ercd));
			
			wdog_ctrl.wdog_load = 0;
			wdog_ctrl.reset_enable = D_DD_PMC_WDOG_ENABLE_ON;
			wdog_ctrl.int_enable = D_DD_PMC_WDOG_ENABLE_OFF;
			ercd = Dd_PMC_Wdog_Ctrl(&wdog_ctrl);
			Ddim_Print(("Dd_PMC_Wdog_Ctrl Error = 0x%08x\n", ercd));
			
			wdog_ctrl.wdog_load = 0xFFFFFFFF;
			wdog_ctrl.reset_enable = D_DD_PMC_WDOG_ENABLE_ON + 1;
			wdog_ctrl.int_enable = D_DD_PMC_WDOG_ENABLE_OFF;
			ercd = Dd_PMC_Wdog_Ctrl(&wdog_ctrl);
			Ddim_Print(("Dd_PMC_Wdog_Ctrl Error = 0x%08x\n", ercd));
			
			wdog_ctrl.wdog_load = 0xFFFFFFFF;
			wdog_ctrl.reset_enable = D_DD_PMC_WDOG_ENABLE_OFF;
			wdog_ctrl.int_enable = D_DD_PMC_WDOG_ENABLE_OFF + 1;
			ercd = Dd_PMC_Wdog_Ctrl(&wdog_ctrl);
			Ddim_Print(("Dd_PMC_Wdog_Ctrl Error = 0x%08x\n", ercd));
			
			Ddim_Print(("Dd_PMC_Wdog_Get_Load Error Test\n"));
			ercd = Dd_PMC_Wdog_Get_Load(NULL);
			Ddim_Print(("Dd_PMC_Wdog_Get_Load Error = 0x%08x\n", ercd));
			
			Ddim_Print(("Dd_PMC_Wdog_Get_Counter Error Test\n"));
			ercd = Dd_PMC_Wdog_Get_Counter(NULL);
			Ddim_Print(("Dd_PMC_Wdog_Get_Counter Error = 0x%08x\n", ercd));
			
			Ddim_Print(("Dd_PMC_Wdog_Get_Control Error Test\n"));
			ercd = Dd_PMC_Wdog_Get_Control(NULL);
			Ddim_Print(("Dd_PMC_Wdog_Get_Control Error = 0x%08x\n", ercd));
			
			Ddim_Print(("Dd_PMC_Wdog_Get_Interrupt Error Test\n"));
			ercd = Dd_PMC_Wdog_Get_Interrupt(NULL, &wdog_int);
			Ddim_Print(("Dd_PMC_Wdog_Get_Interrupt Error = 0x%08x\n", ercd));
			ercd = Dd_PMC_Wdog_Get_Interrupt(&raw_wdog_int, NULL);
			Ddim_Print(("Dd_PMC_Wdog_Get_Interrupt Error = 0x%08x\n", ercd));
			ercd = Dd_PMC_Wdog_Get_Interrupt(NULL, NULL);
			Ddim_Print(("Dd_PMC_Wdog_Get_Interrupt Error = 0x%08x\n", ercd));
			
			Ddim_Print(("Dd_PMC_Wdog_Get_Lock Error Test\n"));
			ercd = Dd_PMC_Wdog_Get_Lock(NULL);
			Ddim_Print(("Dd_PMC_Wdog_Get_Lock Error = 0x%08x\n", ercd));
			
			Ddim_Print(("Dd_PMC_Wdog_Get_Test_Mode Error Test\n"));
			ercd = Dd_PMC_Wdog_Get_Test_Mode(NULL);
			Ddim_Print(("Dd_PMC_Wdog_Get_Test_Mode Error = 0x%08x\n", ercd));
			
			Ddim_Print(("Dd_PMC_Wdog_Get_Peri_Identification Error Test\n"));
			ercd = Dd_PMC_Wdog_Get_Peri_Identification(NULL);
			Ddim_Print(("Dd_PMC_Wdog_Get_Peri_Identification Error = 0x%08x\n", ercd));
			
			Ddim_Print(("Dd_PMC_Wdog_Get_PCell_Identification Error Test\n"));
			ercd = Dd_PMC_Wdog_Get_PCell_Identification(NULL);
			Ddim_Print(("Dd_PMC_Wdog_Get_PCell_Identification Error = 0x%08x\n", ercd));
			
			Ddim_Print(("Dd_PMC_Wdog_Set_Control Error Test\n"));
			ercd = Dd_PMC_Wdog_Set_Control(NULL);
			Ddim_Print(("Dd_PMC_Wdog_Set_Control Error = 0x%08x\n", ercd));
			
			Ddim_Print(("Dd_PMC_Wdog_Set_Lock Error Test\n"));
			ercd = Dd_PMC_Wdog_Set_Lock(D_DD_PMC_WDOG_ENABLE_ON + 1);
			Ddim_Print(("Dd_PMC_Wdog_Set_Lock Error = 0x%08x\n", ercd));
			
			Ddim_Print(("Dd_PMC_Wdog_Set_Test_Mode Error Test\n"));
			ercd = Dd_PMC_Wdog_Set_Test_Mode(D_DD_PMC_WDOG_ENABLE_ON + 1, D_DD_PMC_WDOG_ENABLE_ON, D_DD_PMC_WDOG_ENABLE_OFF);
			Ddim_Print(("Dd_PMC_Wdog_Set_Test_Mode Error = 0x%08x\n", ercd));
			ercd = Dd_PMC_Wdog_Set_Test_Mode(D_DD_PMC_WDOG_ENABLE_OFF, D_DD_PMC_WDOG_ENABLE_ON + 1, D_DD_PMC_WDOG_ENABLE_ON);
			Ddim_Print(("Dd_PMC_Wdog_Set_Test_Mode Error = 0x%08x\n", ercd));
			ercd = Dd_PMC_Wdog_Set_Test_Mode(D_DD_PMC_WDOG_ENABLE_ON, D_DD_PMC_WDOG_ENABLE_OFF, D_DD_PMC_WDOG_ENABLE_ON + 1);
			Ddim_Print(("Dd_PMC_Wdog_Set_Test_Mode Error = 0x%08x\n", ercd));
			
			Ddim_Print(("Dd_PMC_Wdog_Set_Timer Error Test\n"));
			ercd = Dd_PMC_Wdog_Set_Timer(2, 1000);
			Ddim_Print(("Dd_PMC_Wdog_Set_Timer Error = 0x%08x\n", ercd));
			ercd = Dd_PMC_Wdog_Set_Timer(1, 0);
			Ddim_Print(("Dd_PMC_Wdog_Set_Timer Error = 0x%08x\n", ercd));
			
			Ddim_Print(("Dd_PMC_Wdog_Calculate Error Test\n"));
			ercd = Dd_PMC_Wdog_Calculate(1, 1000, NULL);
			Ddim_Print(("Dd_PMC_Wdog_Calculate Error = 0x%08x\n", ercd));
			
			break;
			
		case 7:		// Int_Handler
			Ddim_Print(("Dd_PMC_Wdog_Int_Handler Test Start\n"));
			Dd_PMC_Wdog_Set_Timer(0, 1000);  // (PCLK = 50MHz) -> (gDD_Wdog_Soft_Counter = 0)
			Dd_PMC_Wdog_Int_Handler();
			// WdogLoad
			Dd_PMC_Wdog_Get_Load(&wdog_load);
			Ddim_Print(("WdogLoad = 0x%08x\n", wdog_load));
			
			Dd_PMC_Wdog_Set_Timer(0, 300000);  // (PCLK = 50MHz) -> (gDD_Wdog_Soft_Counter = 3)
			Dd_PMC_Wdog_Int_Handler();
			// WdogLoad
			Dd_PMC_Wdog_Get_Load(&wdog_load);
			Ddim_Print(("WdogLoad = 0x%08x\n", wdog_load));
			
			Dd_PMC_Wdog_Int_Handler();
			// WdogLoad
			Dd_PMC_Wdog_Get_Load(&wdog_load);
			Ddim_Print(("WdogLoad = 0x%08x\n", wdog_load));
			
			Dd_PMC_Wdog_Int_Handler();
			// WdogLoad
			Dd_PMC_Wdog_Get_Load(&wdog_load);
			Ddim_Print(("WdogLoad = 0x%08x\n", wdog_load));
			
			Dd_PMC_Wdog_Int_Handler();
			// WdogLoad
			Dd_PMC_Wdog_Get_Load(&wdog_load);
			Ddim_Print(("WdogLoad = 0x%08x\n", wdog_load));
			Ddim_Print(("Dd_PMC_Wdog_Int_Handler Test End\n"));
			
			break;
			
		default :
			Ddim_Print(("please check 1st parameter!!\n"));
	}
}
